/* Tables1And3.do */

************ Means in counts. ************ 
tempname fh
file open `fh' using "$TABLELOC/SummaryStatsMeansCounts.tex", write replace
* Header rows.
file write `fh' " & FL & GA & MI & NV & OH & PA \\" _newline "\hline" _newline 
local row = "Total precincts"
* Precinct counts
levelsof state, local(states)
foreach st of local states {
  di "`st' "
  qui count if state == "`st'"
  local row = "`row' & `r(N)'"
}
file write `fh' "`row' \\" _newline

* Election results stats.
file write `fh' "\hline" _newline "\multicolumn{7}{c}{Election results} \\" _newline "\hline" _newline
local fmt "%9.0f"
* Dem pct.
local row1 = "Mean Democratic votes 2012"
local row2 = "Mean Democratic votes 2016"
local row3 = "Difference Democratic votes"
foreach st of local states {
  qui summ Dem_Voters_2012_Prct if state == "`st'"
  local v1 = r(mean)
  local row1 = "`row1' & " + string(`v1',"`fmt'")
  qui summ Dem_Voters_2016_Prct if state == "`st'"
  local v2 = r(mean)
  local row2 = "`row2' & " + string(`v2',"`fmt'")
  local ddem_`st' = `v2' - `v1'
  local row3 = "`row3' & " + string(`ddem_`st'',"`fmt'")
}
file write `fh' "`row1' \\" _newline
file write `fh' "`row2' \\" _newline
file write `fh' "`row3' \\" _newline

* GOP pct.
local row1 = "Mean Republican votes 2012"
local row2 = "Mean Republican votes 2016"
local row3 = "Difference Republican votes"
foreach st of local states {
  qui summ GOP_Voters_2012_Prct if state == "`st'"
  local v1 = r(mean)
  local row1 = "`row1' & " + string(`v1',"`fmt'")
  qui summ GOP_Voters_2016_Prct if state == "`st'"
  local v2 = r(mean)
  local row2 = "`row2' & " + string(`v2',"`fmt'")
  local dgop_`st' = `v2' - `v1'
  local row3 = "`row3' & " + string(`dgop_`st'',"`fmt'")
}
file write `fh' "`row1' \\" _newline
file write `fh' "`row2' \\" _newline
file write `fh' "`row3' \\" _newline
file write `fh' "\hline" _newline 
* Difference in difference.
local rowdid = "Difference in difference votes"
foreach st of local states {
  local did = `dgop_`st'' - `ddem_`st''
  local rowdid = "`rowdid' & " + string(`did',"`fmt'")
}
file write `fh' "`rowdid' \\" _newline

* Turnout stats.
file write `fh' "\hline" _newline "\multicolumn{7}{c}{Composition by party registration/primary participation} \\" _newline "\hline" _newline
local fmt "%9.0f"
* Turnout counts.
qui gen GOPTURNOUTNUM12 = P12_V12_GOP + PBOTH_V12_GOP + PBOTH_VBOTH_GOP
qui gen GOPTURNOUTNUM16 = P16_V16_GOP + PBOTH_V16_GOP + PBOTH_VBOTH_GOP
qui gen DEMTURNOUTNUM12 = P12_V12_DEM + PBOTH_V12_DEM + PBOTH_VBOTH_DEM
qui gen DEMTURNOUTNUM16 = P16_V16_DEM + PBOTH_V16_DEM + PBOTH_VBOTH_DEM

* Dem turnout.
local row1 = "Mean Democratic voters 2012"
local row2 = "Mean Democratic voters 2016"
local row3 = "Difference Democratic voters"
foreach st of local states {
  qui summ DEMTURNOUTNUM12 if state == "`st'"
  local v1 = r(mean)
  local row1 = "`row1' & " + string(`v1',"`fmt'")
  qui summ DEMTURNOUTNUM16 if state == "`st'"
  local v2 = r(mean)
  local row2 = "`row2' & " + string(`v2',"`fmt'")
  local ddem_`st' = `v2' - `v1'
  local row3 = "`row3' & " + string(`ddem_`st'',"`fmt'")
}
file write `fh' "`row1' \\" _newline
file write `fh' "`row2' \\" _newline
file write `fh' "`row3' \\" _newline

* Rep turnout.
local row1 = "Mean Republican voters 2012"
local row2 = "Mean Republican voters 2016"
local row3 = "Difference Republican voters"
foreach st of local states {
  qui summ GOPTURNOUTNUM12 if state == "`st'"
  local v1 = r(mean)
  local row1 = "`row1' & " + string(`v1',"`fmt'")
  qui summ GOPTURNOUTNUM16 if state == "`st'"
  local v2 = r(mean)
  local row2 = "`row2' & " + string(`v2',"`fmt'")
  local dgop_`st' = `v2' - `v1'
  local row3 = "`row3' & " + string(`dgop_`st'',"`fmt'")
}
file write `fh' "`row1' \\" _newline
file write `fh' "`row2' \\" _newline
file write `fh' "`row3' \\" _newline
file write `fh' "\hline" _newline 
* Difference in difference.
local rowdid = "Difference in difference voters"
foreach st of local states {
  local did = `dgop_`st'' - `ddem_`st''
  local rowdid = "`rowdid' & " + string(`did',"`fmt'")
}
file write `fh' "`rowdid' \\" _newline

file write `fh' "\hline" _newline
file close `fh'

************ Means in pct shares. ************ 
tempname fh
file open `fh' using "$TABLELOC/SummaryStatsMeansShares.tex", write replace
* Header rows.
file write `fh' " & FL & GA & MI & NV & OH & PA \\" _newline "\hline" _newline 
local row = "Total precincts"
* Precinct counts
levelsof state, local(states)
foreach st of local states {
  qui count if state == "`st'"
  local row = "`row' & `r(N)'"
}
file write `fh' "`row' \\" _newline

* Election results stats.
file write `fh' "\hline" _newline "\multicolumn{7}{c}{Election results} \\" _newline "\hline" _newline
local fmt "%9.1f"
* Dem pct.
local row1 = "Mean Democratic vote share 2012"
local row2 = "Mean Democratic vote share 2016"
local row3 = "Difference Democratic vote share"
foreach st of local states {
  qui summ DEMPCT12 if state == "`st'"
  local v1 = r(mean)
  local row1 = "`row1' & " + string(`v1',"`fmt'")
  qui summ DEMPCT16 if state == "`st'"
  local v2 = r(mean)
  local row2 = "`row2' & " + string(`v2',"`fmt'")
  local ddem_`st' = `v2' - `v1'
  local row3 = "`row3' & " + string(`ddem_`st'',"`fmt'")
}
file write `fh' "`row1' \\" _newline
file write `fh' "`row2' \\" _newline
file write `fh' "`row3' \\" _newline

* GOP pct.
local row1 = "Mean Republican vote share 2012"
local row2 = "Mean Republican vote share 2016"
local row3 = "Difference Republican vote share"
foreach st of local states {
  qui summ GOPPCT12 if state == "`st'"
  local v1 = r(mean)
  local row1 = "`row1' & " + string(`v1',"`fmt'")
  qui summ GOPPCT16 if state == "`st'"
  local v2 = r(mean)
  local row2 = "`row2' & " + string(`v2',"`fmt'")
  local dgop_`st' = `v2' - `v1'
  local row3 = "`row3' & " + string(`dgop_`st'',"`fmt'")
}
file write `fh' "`row1' \\" _newline
file write `fh' "`row2' \\" _newline
file write `fh' "`row3' \\" _newline
file write `fh' "\hline" _newline 
* Difference in difference.
local rowdid = "Difference in difference vote share"
foreach st of local states {
  local did = `dgop_`st'' - `ddem_`st''
  local rowdid = "`rowdid' & " + string(`did',"`fmt'")
}
file write `fh' "`rowdid' \\" _newline

* Turnout stats.
file write `fh' "\hline" _newline "\multicolumn{7}{c}{Composition by party registration/primary participation} \\" _newline "\hline" _newline
local fmt "%9.1f"
* Dem turnout.
local row1 = "Mean Democratic composition share 2012"
local row2 = "Mean Democratic composition share 2016"
local row3 = "Difference Democratic composition share"
foreach st of local states {
  qui summ DEMTURNOUTPCT12 if state == "`st'"
  local v1 = r(mean)
  local row1 = "`row1' & " + string(`v1',"`fmt'")
  qui summ DEMTURNOUTPCT16 if state == "`st'"
  local v2 = r(mean)
  local row2 = "`row2' & " + string(`v2',"`fmt'")
  local ddem_`st' = `v2' - `v1'
  local row3 = "`row3' & " + string(`ddem_`st'',"`fmt'")
}
file write `fh' "`row1' \\" _newline
file write `fh' "`row2' \\" _newline
file write `fh' "`row3' \\" _newline

* Rep turnout.
local row1 = "Mean Republican composition share 2012"
local row2 = "Mean Republican composition share 2016"
local row3 = "Difference Republican composition share "
foreach st of local states {
  qui summ GOPTURNOUTPCT12 if state == "`st'"
  local v1 = r(mean)
  local row1 = "`row1' & " + string(`v1',"`fmt'")
  qui summ GOPTURNOUTPCT16 if state == "`st'"
  local v2 = r(mean)
  local row2 = "`row2' & " + string(`v2',"`fmt'")
  local dgop_`st' = `v2' - `v1'
  local row3 = "`row3' & " + string(`dgop_`st'',"`fmt'")
}
file write `fh' "`row1' \\" _newline
file write `fh' "`row2' \\" _newline
file write `fh' "`row3' \\" _newline
file write `fh' "\hline" _newline 
* Difference in difference.
local rowdid = "Difference in difference composition share"
foreach st of local states {
  local did = `dgop_`st'' - `ddem_`st''
  local rowdid = "`rowdid' & " + string(`did',"`fmt'")
}
file write `fh' "`rowdid' \\" _newline

file write `fh' "\hline" _newline
file close `fh'
